/*
 * Copyright 2014 Alex Curran
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.espian.showcaseview.sample;

import com.espian.showcaseview.sample.animations.AnimationSampleAbility;
import com.github.amlcurran.showcaseview.OnShowcaseEventListener;
import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.targets.ViewTarget;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ListContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Arrays;

/**
 * 主界面
 */
public class MainAbility extends Ability implements OnShowcaseEventListener, Component.ClickedListener {
    private ShowcaseView sv;
    private Button buttonBlocked;
    private ListContainer listView;
    private boolean isFirstTheme = true;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_main, null, false);
        setUIContent(componentContainer);
        buttonBlocked = (Button) findComponentById(ResourceTable.Id_buttonBlocked);
        buttonBlocked.setClickedListener(this);
        listView = (ListContainer) findComponentById(ResourceTable.Id_listView);
        listView.setItemProvider(new MyListProvider(this));
        ViewTarget target = new ViewTarget(ResourceTable.Id_buttonBlocked, this);

        sv = new ShowcaseView.Builder(this, (ComponentContainer) componentContainer.getComponentParent())
                .withMaterialShowcase()
                .setTarget(target)
                .setContentTitle(ResourceTable.String_showcase_main_title)
                .setContentText(ResourceTable.String_showcase_main_message)
                .setStyle(ResourceTable.Pattern_CustomShowcaseTheme2)
                .setShowcaseEventListener(this)
                .replaceEndButton(ResourceTable.Layout_view_custom_button)
                .build();
        DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig
                (DependentLayout.LayoutConfig.MATCH_CONTENT,
                        DependentLayout.LayoutConfig.MATCH_CONTENT);
        layoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
        layoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_RIGHT);
        int margin = 20 * getResourceManager().getDeviceCapability().screenDensity / 160;
        layoutConfig.setMargins(margin, margin, margin, margin);
        sv.setButtonPosition(layoutConfig);

        listView.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName(DemoOption.values()[i].activityClass.getCanonicalName())
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });
    }

    @Override
    public void onShowcaseViewHide(ShowcaseView showcaseView) {
        buttonBlocked.setText(ResourceTable.String_button_show);
    }

    @Override
    public void onShowcaseViewDidHide(ShowcaseView showcaseView) {
    }

    @Override
    public void onShowcaseViewShow(ShowcaseView showcaseView) {
        buttonBlocked.setText(ResourceTable.String_button_hide);
    }

    @Override
    public void onShowcaseViewTouchBlocked(TouchEvent motionEvent) {
    }

    @Override
    public void onClick(Component component) {
        int viewId = component.getId();
        switch (viewId) {
            case ResourceTable.Id_buttonBlocked:
                if (sv.isShowing()) {
                    if (isFirstTheme) {
                        sv.setStyle(ResourceTable.Pattern_CustomShowcaseTheme);
                        isFirstTheme = false;
                    } else {
                        sv.setStyle(ResourceTable.Pattern_CustomShowcaseTheme2);
                        isFirstTheme = true;
                    }
                } else {
                    sv.show();
                }
                break;
        }
    }


    private static class MyListProvider extends EasyListProvider<DemoOption> {
        private MyListProvider(Context context) {
            super(context, ResourceTable.Layout_item_next_thing,
                    Arrays.asList(DemoOption.values()));
        }

        @Override
        protected void bind(ViewHolder holder, DemoOption option, int position) {
            Text text = holder.getView(ResourceTable.Id_textView);
            text.setText(option.titleRes);
            Text text2 = holder.getView(ResourceTable.Id_textView2);
            text2.setText(option.summaryRes);
        }
    }

    private enum DemoOption {
        FRAGMENTS(ResourceTable.String_title_fragments, ResourceTable.String_sum_fragments, FragmentDemoAbility.class),
        EVENTS(ResourceTable.String_title_events, ResourceTable.String_sum_event, EventsAbility.class),
        SINGLE_SHOT(ResourceTable.String_title_single_shot, ResourceTable.String_sum_single_shot, SingleShotAbility.class),
        ANIMATIONS(ResourceTable.String_title_animations, ResourceTable.String_sum_animations, AnimationSampleAbility.class),
        CUSTOM_TEXT(ResourceTable.String_custom_text, ResourceTable.String_custom_text_summary, CustomTextAbility.class),
        CUSTOM_SHOWCASE(ResourceTable.String_custom_showcase_title, ResourceTable.String_custom_showcase_summary, CustomShowcaseAbility.class),
        MEMORY(ResourceTable.String_title_memory, ResourceTable.String_sum_memory, MemoryManagementTesting.class);

        final int titleRes;
        final int summaryRes;
        final Class<? extends Ability> activityClass;

        DemoOption(int titleRes, int summaryRes, Class<? extends Ability> activityClass) {
            this.titleRes = titleRes;
            this.summaryRes = summaryRes;
            this.activityClass = activityClass;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (sv != null) {
            sv.destory();
        }
    }
}
